﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lidgren.Network;

namespace TradeSeaShared
{
    /// <summary>
    /// These are static versions of all the managers in the game
    /// You should be putting references to classes in here.
    /// </summary>
    public class Singiltons
    {
        /// <summary>
        /// If the client is playing single player then we could need to make some changes to how we do stuff.
        /// </summary>
        public static bool IsListen = false;

        public static bool RunServer = false;

        public const string Version = "0.1";

        public const int ApplicationPort = 24234;

        /// <summary>
        /// This is the same for both the client and server, it's just a string to identiy the application
        /// </summary>
        public const string ApplicationName = "TradeSea.waikato." + Version;

        // NetPeerConfig, really important
        public static NetPeerConfiguration ClientNetConfig = new NetPeerConfiguration(ApplicationName);
        public static NetPeerConfiguration ServerNetConfig = new NetPeerConfiguration(ApplicationName);

        public static void Init(bool isListen, bool IsServer = false)
        {
            IsListen = isListen;

            ClientNetConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

            if (isListen)
            {
                Log("Global", "Starting As Listen");
                RunServer = true;
            }

            if (IsServer)
            {
                Log("Global", "Starting As Server");
                RunServer = true;
            }

            if (RunServer)
            {
                ServerNetConfig.Port = ApplicationPort;
                ServerNetConfig.MaximumConnections = 32; // Basicly Max Players
                ServerNetConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            }

            if (!isListen && !IsServer)
            {
                Log("Global", "Starting As Client");
            }
        }

        public static void Log(string type, object message)
        {
            Console.WriteLine(string.Format("[{0}] : {1}", type, message.ToString()));
            if (OnLog != null)
            {
                OnLog(type, message);
            }
        }

        public delegate void LogMethod(string type, object message);
        public static event LogMethod OnLog;
    }
}
